package com.ggh.jinjilive.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.dialog.LoadDialog;
import com.ggh.jinjilive.dialog.PayChooseDialog;
import com.ggh.jinjilive.mobile.UserMobile;
import com.ggh.jinjilive.remotedata.ViewModelFactory;
import com.kingja.loadsir.core.LoadService;

import javax.inject.Inject;

public abstract class BaseFragment<V extends ViewDataBinding> extends Fragment {
    //
    protected Context mContext;
    //
    protected V binding;
    @Inject
    public ViewModelFactory viewModelFactory;
    //用户信息
    protected UserMobile userMobile;
    //
    protected LoadDialog loadDialog;
    //网络问题或者暂无数据
    protected LoadService loadService;
    //支付弹框
    protected PayChooseDialog payChooseDialog;
    //区分是支付宝还是微信
    protected int WeChatAlipay = 0;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != getContext()) {
            mContext = getContext();
        } else if (null != getActivity()) {
            mContext = getActivity();
        } else if (null != BaseApplication.getInstance()) {
            mContext = BaseApplication.getInstance();
        } else {
            return;
        }
        //用户信息
        userMobile = MerchantSession.getInstance().getUser();
        //支付弹框
        payChooseDialog = new PayChooseDialog(mContext);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, initContentView(inflater, container, savedInstanceState), container, false);
        return binding.getRoot();
    }


    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //页面数据初始化方法

        initView();
        initData();
        //页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
        initViewObservable();
    }

    public void initParam() {

    }

    public void initView() {

    }

    public void initData() {

    }

    public void initViewObservable() {

    }


    public void showDialog() {
        if (!getActivity().isFinishing()) {
            if(null != loadDialog && loadDialog.isShowing()){
                return;
            }
            loadDialog = new LoadDialog(mContext);
            try {
                loadDialog.show();
            }catch (Exception e){
                e.getMessage();
            }
        }
    }


    public void cancelDialog() {
        if (loadDialog != null && !getActivity().isFinishing()) {
            loadDialog.dismiss();
        }
    }
}
